package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmManuallyReviewDto;
import com.heima.model.wemedia.dtos.WmWeMediaDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.pojos.WmWeMediaPojo;
import com.heima.model.wemedia.pojos.WmNews;

import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vo.WmNewsVo;
import com.heima.wemedia.mapper.WeMediaMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmNewsAutoScanService;
import com.heima.wemedia.service.WmWeMediaService;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
@Slf4j
public class WmWeMediaServiceImpl extends ServiceImpl<WeMediaMapper, WmNews> implements WmWeMediaService {
    /**
     * 自媒体审核查询
     *
     * @param adWeMediaDto
     * @return
     */
    @Autowired
    private WmUserService wmUserService;

    @Override
    public ResponseResult findAll(WmWeMediaDto adWeMediaDto) {
        //1.检查参数
        if (adWeMediaDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页
        adWeMediaDto.checkParam();
        //分页查询
        Page page = new Page(adWeMediaDto.getPage(), adWeMediaDto.getSize());

        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper();

        //todo 作者名
        // 根据标题模糊查询
        if (StringUtils.isNoneBlank(adWeMediaDto.getTitle())) {
            wrapper.like(WmNews::getTitle, adWeMediaDto.getTitle());
        }

        // 根据状态查询
        if (adWeMediaDto.getStatus() != null)
            wrapper.eq(WmNews::getStatus, adWeMediaDto.getStatus());

        // 根据时间倒叙查询
        wrapper.orderByDesc(WmNews::getPublishTime);

        page(page, wrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(adWeMediaDto.getPage(), adWeMediaDto.getSize(), (int) page.getTotal());
        List<WmNews> wmNews = page.getRecords();//获取所有查询到得数据
        List<WmNewsVo> wmNewsVos = new ArrayList<>();
        for (WmNews wmNew : wmNews) {
            WmNewsVo wmNewsVo = new WmNewsVo();
            BeanUtils.copyProperties(wmNew, wmNewsVo);
            Integer userId = wmNew.getUserId();
            LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(userId != null, WmUser::getId, userId);
            WmUser user = wmUserService.getOne(queryWrapper);
            String name = user.getName();
            wmNewsVo.setAuthorName(name);
            wmNewsVos.add(wmNewsVo);
        }

        pageResponseResult.setData(wmNewsVos);
        return pageResponseResult;
    }

    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private WmUserService service;

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult selectById(Long id) {
        /*LambdaQueryWrapper<WmWeMediaPojo> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(WmNews::getArticleId,id);*/
        //查询文章
        WmNews wmNews = wmNewsMapper.selectById(id);
        //获取userId
        Integer userId = wmNews.getUserId();

        LambdaQueryWrapper<WmUser> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(userId != null, WmUser::getId, userId);
        WmUser wmUser = service.getOne(wrapper);
        String userName = wmUser.getName();
        log.info("查询的数据：{}", wmNews);
        WmWeMediaPojo adWeMediaPojo = new WmWeMediaPojo();
        //拷贝前到后
        BeanUtils.copyProperties(wmNews, adWeMediaPojo);
        adWeMediaPojo.setAuthorName(userName);
        return ResponseResult.okResult(adWeMediaPojo);
    }

    /**
     * 人工审核驳回
     *
     * @param dto
     * @return
     */
    @Autowired
    private WmNewsAutoScanServiceImpl autoScanService;

    @Override
    public ResponseResult auditReject(WmManuallyReviewDto dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, WmNews::getId, id);
        WmNews wmNews = wmNewsMapper.selectOne(wrapper);
        /*wmNews.setStatus(dto.getStatus());
        wmNews.setReason(dto.getMsg());*/
        log.info("====>{}" + wmNews);
        //调用WmNewsAutoScanServiceImpl，updateNews方法，更改
        autoScanService.updateNews(wmNews, WmNews.Status.FAIL.getCode(), dto.getMsg());
        return ResponseResult.okResult(wmNews);
    }

    /**
     * 人工审核成功
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult auditSucceed(WmManuallyReviewDto dto) {
        Long id = dto.getId();
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(id != null, WmNews::getId, id);
        WmNews wmNews = wmNewsMapper.selectOne(wrapper);
        ResponseResult responseResult = autoScanService.saveArticle(wmNews);
        if (responseResult.getCode()==200){
            wmNews.setArticleId((Long) responseResult.getData());
            autoScanService.updateNews(wmNews,WmNews.Status.PUBLISHED.getCode(),"审核成功");
        }
        return ResponseResult.okResult(wmNews);
    }


}
